# arg 1:  the new package version
# arg 2:  the old package version

RELEASE=3.12.24-1
KERNEL_VERSION=3.12.24-MANJARO
KERNEL_BASE=3.12
BASEVER=312
ARCH=x86_64

boobies () {
  echo "prepending CPU microcode to /boot/initramfs-${BASEVER}-${ARCH}.img"
  local destfirst="/tmp/initrd/"
  local destmc="${destfirst}/kernel/x86/microcode/"
  mkdir -p "${destmc}"
  ( cp -f "/lib/firmware/amd-ucode/microcode_amd.bin" "${destmc}/AuthenticAMD.bin" && cd "${destfirst}" && find . | cpio -o -H newc > "../ucode.cpio" 2>/dev/null && cd .. && cat "ucode.cpio" "/boot/initramfs-${BASEVER}-${ARCH}.img" > "/tmp/initramfs-${BASEVER}-${ARCH}.img" && mv -f "/tmp/initramfs-${BASEVER}-${ARCH}.img" "/boot/initramfs-${BASEVER}-${ARCH}.img")
}


post_install () {
  # updating module dependencies
  echo ">>> Updating module dependencies. Please wait ..."
  depmod ${KERNEL_VERSION}
  echo ">>> Generating initial ramdisk, using mkinitcpio.  Please wait..."
  mkinitcpio -p linux${BASEVER}
  
  boobies

  # update grub.cfg
  if [ -f /usr/sbin/update-grub ]; then
      update-grub
  else
      echo "WARNING: It seems that grub is not installed - Your system might not boot."
      echo "Add followed initramfs files to your bootloader config:"
      echo ">> linux	/boot/vmlinuz-${BASEVER}-${ARCH}"
      echo ">> initrd	/boot/initramfs-${BASEVER}-${ARCH}.img"
  fi
}

post_upgrade() {
  # reminder notice
  if findmnt --fstab -uno SOURCE /boot &>/dev/null && ! mountpoint -q /boot; then
      echo "WARNING: /boot appears to be a separate partition but is not mounted."
  fi
  
  # remove old initcpio
  rm -f /boot/initramfs-${KERNEL_BASE}*-${ARCH}*.img
  rm -f /boot/initramfs-${BASEVER}-${ARCH}*.img
  
  # updating module dependencies
  echo ">>> Updating module dependencies. Please wait ..."
  depmod ${KERNEL_VERSION}
  echo ">>> Generating initial ramdisk, using mkinitcpio.  Please wait..."
  mkinitcpio -p linux${BASEVER}
  
  boobies

  # update grub.cfg
  if [ -f /usr/sbin/update-grub ]; then
      update-grub
  else
      echo "WARNING: It seems that grub is not installed - Your system might not boot."
      echo "Add followed initramfs files to your bootloader config:"
      echo ">> linux	/boot/vmlinuz-${BASEVER}-${ARCH}"
      echo ">> initrd	/boot/initramfs-${BASEVER}-${ARCH}.img"
  fi
}

post_remove() {
  # remove initcpio
  rm -f /boot/initramfs-${BASEVER}-${ARCH}*.img
  
  # update grub.cfg
  if [ -f /usr/sbin/update-grub ]; then
      update-grub
  fi
}
